import streamlit as st
import os
from .downloaders import *
from .base_downloader import BaseDownloader


def get_browser_name_for_site_name(site_name: str) -> str:
    """Get the browser name for a given site name."""
    downloader_class = globals().get(site_name)
    browser_name = (
        downloader_class().browser_name if downloader_class else "🌐 Not Found"
    )
    return browser_name


def configure_page_navigation(current_page):
    """Configure the navigation sidebar for all pages"""
    # Add sidebar title
    st.sidebar.markdown("# 🤖 Besins Downloaders")

    # Pages configuration
    pages_config = {
        "app": {"title": "🏠 Dashboard", "icon": "🏠"},
        "log_viewer": {"title": "📋 Log Viewer", "icon": "📋"},
        "run_one_website": {"title": "▶️ Run Single Site", "icon": "▶️"},
        "run_remaining_websites": {"title": "⏩ Run Remaining", "icon": "⏩"},
        "run_all_websites": {"title": "🔄 Run All Sites", "icon": "🔄"},
        "screenshots_viewer": {"title": "📸 Screenshots", "icon": "📸"},
        "xlsx_viewer": {"title": "📊 Excel Viewer", "icon": "📊"},
        "config_editor": {"title": "⚙️ Configurations", "icon": "⚙️"},
        "readme": {"title": "📖 Readme", "icon": "📖"},
    }

    css_path = os.path.join(os.path.dirname(__file__), "streamlit_app/css/sidebar_navigation.css")
    with open(css_path) as f:
        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

    # Create custom navigation menu
    nav_html = '<div class="sidebar-nav">'
    for page_id, page_info in pages_config.items():
        active_class = "active" if page_id == current_page else ""
        nav_html += f'<a href="{page_id}" class="nav-link {active_class}">{page_info["icon"]} {page_info["title"].replace(page_info["icon"] + " ", "")}</a>'
    nav_html += "</div>"

    # Render the navigation
    st.sidebar.markdown(nav_html, unsafe_allow_html=True)
